package com.yz.forlogin.system.repository;

import com.yz.forlogin.security.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author shuang.kou
 */
public interface UserRepository extends JpaRepository<User, Integer> {
    Optional<User> findByUsername(String username);

    @Transactional
    void deleteByUsername(String username);

    @Transactional
    @Modifying
    @Query(nativeQuery = true,value = "UPDATE t_user SET last_login = NOW() WHERE id = ?1")
    void updatelastLogin(Integer id);

    @Query(value = "SELECT * FROM t_user WHERE username = ?1",nativeQuery = true)
    User getUserInfoByUsername(String username);
}
